import os
import platform
import joblib


class project_tools:
    insects_img_dirname = "insects_imgs"
    processed_img_dirname = "processed_imgs"
    characteristic_dirname = "chas_data"
    models_dirname = "trainning_models"
    insects_tags_dirname = "insects_tags"
    trainning_source_dirname = "trainning_data_source"
    single_img_processing_dirname = "processed_single_img"
    model_name = "Model4April.pkl"
    system_platform = platform.system().lower()
    supported_filetype = [".jpg", ".jpeg", ".png", ".tif", ".bmp", ".gif",
                          ".mp4", ".rmvb", ".wmv", ".mkv", ".avi",
                          ".doc", ".docx", ".xls", ".xml", ".dot", ".ppt",
                          ".txt", ".pdf", ".pptx", ".xlsm", ".xlsx", ".log",
                          ".zip", ".rar", ".7z"]

    @staticmethod
    def get_system_platform():
        '''
        判断当前系统运行平台，用于filemanager.py /scan/ 接口针对windows和linux不同路径格式进行处理
        :return:
        '''
        return platform.system().lower()

    @staticmethod
    def project_root_path(project_name=None):
        """
        获取当前项目根路径
        :param project_name:
        :return: 根路径
        """
        PROJECT_NAME = 'insects_detection_flask_backend2' if project_name is None else project_name
        project_path = os.path.abspath(os.path.dirname(__file__))
        root_path = project_path[:project_path.find(PROJECT_NAME) + len(PROJECT_NAME)]
        return root_path

    @staticmethod
    def prepare_working_dirs(project_root_dir):
        if not os.path.exists(project_root_dir + os.sep + project_tools.insects_img_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.insects_img_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.processed_img_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.processed_img_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.characteristic_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.characteristic_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.models_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.models_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.insects_tags_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.insects_tags_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.trainning_source_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.trainning_source_dirname)
        if not os.path.exists(project_root_dir + os.sep + project_tools.single_img_processing_dirname):
            os.mkdir(project_root_dir + os.sep + project_tools.single_img_processing_dirname)

    @staticmethod
    def save_trained_models(model, model_save_path, model_name):
        joblib.dump(model, model_save_path + model_name)
        print(model_name + "已写入")

    @staticmethod
    # 依据原图像存储的路径,在目标路径创建相同的父文件夹
    def prepare_img_output_dir(source_img_dir, target_img_dir):
        origin_folder_name = source_img_dir.rsplit(os.sep, 1)[1]
        if not os.path.exists(target_img_dir + os.sep + origin_folder_name):
            os.mkdir(target_img_dir + os.sep + origin_folder_name)
        return target_img_dir + os.sep + origin_folder_name

    @staticmethod
    def convert_file_size(file_size):
        mb_size = file_size / (1024 * 1024)
        kb_size = file_size / 1024
        if mb_size < 0.9:
            return str(round(kb_size)) + " K"
        else:
            return str(round(mb_size)) + " M"
